package org.projectwithoutname.ui.wizard.pages.composite;



import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.projectwithoutname.commons.Commons;

public class ImageComposite extends Composite implements Commons{

	private ImageData imageData = null;
	private Image image = null;
	private int x, y;
	private float i;
	private Label imageLabel;
	private Rectangle rectangle, oldRectangle;
			
	public ImageComposite(Composite parent, int style, ImageData imageData) {
		super(parent, style);
		this.imageData = imageData;
		oldRectangle = new Rectangle(0, 0, 0, 0);
		initialize();
	}
	
	private void initialize() {
		x = imageData.width;
		y = imageData.height;
		if (imageData.height < HEIGHT){
			for (i=1.1F; i<=3; i +=0.1F){
				if((int)(imageData.height * i) >= HEIGHT){
					y = (int) (imageData.height * i);
					x = (int) (imageData.width * i);
					break;
				}
			}
		}
		else{
			if (imageData.height > HEIGHT){
				for (i=1.0F; i>0; i -=0.1F){
					if((int)(imageData.height * i) <= HEIGHT){
						y = (int) (imageData.height * i);
						x = (int) (imageData.width * i);
						break;
					}
				}
			}
		}
		imageData = imageData.scaledTo(x, y);
		imageLabel = new Label(this, SWT.NONE);
		rectangle = getPoints(imageData);
		setOldRectangle(rectangle);
		imageData = setAlpha(imageData);
		image = new Image(getDisplay(), imageData);
		imageLabel.setImage(image);
		setLayout(new FillLayout());
	}
	
	public void refresh(){
		image.dispose();
		imageData = setNewAlpha(imageData);
		image = new Image(getDisplay(), imageData);
		imageLabel.setImage(image);
		
	}
	
	private Rectangle getPoints(ImageData imageData){
		int x = SELECTION_WIDTH;
		int y = SELECTION_HEIGHT;
		int aux = (int)(imageData.width * 0.5);
		System.out.println("aux: "+aux);
		if (aux > SELECTION_WIDTH){
			for (int i=1; i<=50; i +=1){
				if((int)(SELECTION_WIDTH * i) >= aux){
					y = SELECTION_HEIGHT * i;
					x = SELECTION_WIDTH * i;
					break;
				}
			}
		}
		int hHalf = (int) (imageData.width/2);
		int vHalf = (int) (imageData.height/2);
		
		int startX = (int)(hHalf - (x/2));
		int startY = (int)(vHalf - (y/2));
		int endX = startX + x;
		int endY = startY + y;
		
		Rectangle rectangle = new Rectangle(startX, startY, endX, endY);
		return rectangle;
	}
	
	private ImageData setAlpha(ImageData imageData){
		for (int y=0; y<imageData.height; y++){
			for(int x=0; x<imageData.width; x++){
				if ((x < rectangle.x) || (x >= rectangle.width) || (y < rectangle.y) || (y >= rectangle.height))
					imageData.setAlpha(x, y, 128);
				else
					imageData.setAlpha(x, y, 255);
			}
		}
		return imageData;
	}
	
	private void setOldRectangle(Rectangle rectangle){
		oldRectangle.x = rectangle.x;
		oldRectangle.width = rectangle.width;
		oldRectangle.y = rectangle.y;
		oldRectangle.height = rectangle.height;
		
	}
	
	public ImageData setNewAlpha(ImageData imageData){
		Rectangle intersection = oldRectangle.intersection(rectangle);
		Rectangle union = oldRectangle.union(rectangle);
		Rectangle modify = union.intersection(rectangle);
		Rectangle dispose = union.intersection(oldRectangle);
		
		for (int y=0; y<imageData.height; y++){
			for(int x=0; x<imageData.width; x++){
				if ((x >= modify.x) && (x < modify.width) && (y >= modify.y) && (y < modify.height)){
					if (!((x > intersection.x) && (x < intersection.width) && (y > intersection.y) && (y < intersection.height))){
						imageData.setAlpha(x, y, 255);
					}
				}
				else{
					if ((x >= dispose.x) && (x < dispose.width) && (y >= dispose.y) && (y < dispose.height)){
						if (!((x > intersection.x) && (x < intersection.width) && (y > intersection.y) && (y < intersection.height))){
							imageData.setAlpha(x, y, 128);
						}
					}
				}
			}
		}
		setOldRectangle(rectangle);
		return imageData;
	}
	
	public Image getImage(){
		return image;
	}
	
	public Label getImageLabel(){
		return imageLabel;
	}
	
	public void setRectangle(Rectangle rectangle){
		this.rectangle = rectangle;
		refresh();
	}
	
	public Rectangle getRectangle(){
		return rectangle;
	}
	

}
